#ifndef TASK_H
#define TASK_H

#include <QObject>
#include <QDateTime>


#include "types.h"

class Task : public QObject
{
    Q_OBJECT
    Q_PROPERTY(uint64_t id READ getId CONSTANT)
    Q_PROPERTY(bool isEnabled READ isEnabled WRITE setEnabled NOTIFY isEnabledChanged)
    Q_PROPERTY(Types::TaskType taskType READ getTaskType WRITE setTaskType NOTIFY taskTypeChanged)
    Q_PROPERTY(Types::DrivingCurrent current READ getCurrent WRITE setCurrent NOTIFY taskTypeChanged)
    Q_PROPERTY(QDateTime startTime READ getStartTime WRITE setStartTime NOTIFY startTimeChanged)
    Q_PROPERTY(bool repeat READ isRepeat WRITE setRepeat NOTIFY repeatChanged)
    Q_PROPERTY(quint64 interval READ getInterval WRITE setInterval NOTIFY intervalChanged)
    Q_PROPERTY(quint16 freq READ getFreq WRITE setFreq NOTIFY freqChanged)
    Q_PROPERTY(quint8 priority READ getPriority WRITE setPriority NOTIFY priorityChanged)

public:
    explicit Task(quint64 id, QObject *parent = nullptr)
        : QObject(parent), is_enabled(true), id(id), taskType(Types::GET_VOLTAGE), startTime(QDateTime::currentDateTime()), repeat(true), interval(5000), freq(0) {
        };
    virtual ~Task() {}
    quint8 priority = 0; // 0: low

signals:
    void isEnabledChanged(bool enabled);
    void taskTypeChanged(Types::TaskType type);
    void startTimeChanged(const QDateTime &time);
    void repeatChanged(bool repeat);
    void intervalChanged(quint64 interval);
    void freqChanged(quint16 freq);
    void priorityChanged(quint8 priority);

public:
    // bool isEnabled() const { return is_enabled; }
    bool isEnabled() const { return is_enabled; }
    uint64_t getId() const { return id; }
    Types::TaskType getTaskType() const { return taskType; }
    Types::DrivingCurrent getCurrent() const { return current; }
    QDateTime getStartTime() const { return startTime; }
    bool isRepeat() const { return repeat; }
    quint64 getInterval() const { return interval; }
    quint16 getFreq() const { return freq; }
    quint8 getPriority() const { return priority; }

    Q_INVOKABLE void upload();

    void setEnabled(bool enabled)
    {
        if (is_enabled != enabled)
        {
            is_enabled = enabled;
            emit isEnabledChanged(enabled);
        }
    }
    void setTaskType(Types::TaskType type)
    {
        if (taskType != type)
        {
            taskType = type;
            emit taskTypeChanged(type);
        }
    }

    void setCurrent(Types::DrivingCurrent c)
    {
        if (current != c)
        {
            current = c;
            emit taskTypeChanged(taskType); // Emit task type change to update current
        }
    }

    void setStartTime(const QDateTime &time)
    {
        if (startTime != time)
        {
            startTime = time;
            emit startTimeChanged(time);
        }
    }

    void setRepeat(bool r)
    {
        if (repeat != r)
        {
            repeat = r;
            emit repeatChanged(r);
        }
    }

    void setInterval(quint64 i)
    {
        if (interval != i)
        {
            interval = i;
            emit intervalChanged(i);
        }
    }

    void setFreq(quint16 f)
    {
        if (freq != f)
        {
            freq = f;
            emit freqChanged(f);
        }
    }

    void setPriority(quint8 p)
    {
        if (priority != p)
        {
            priority = p;
            emit priorityChanged(p);
        }
    }

private:
    bool is_enabled;
    uint64_t id;
    Types::TaskType taskType;
    Types::DrivingCurrent current;
    QDateTime startTime;
    bool repeat = false;
    quint64 interval; // ms
    quint16 freq;
};

#endif // TASK_H
